fs = require("fs")
path = require("path")

module.exports = (grunt) ->

  jekyllSiteDist = path.join(__dirname, '../', '_site/public/dist')

  # ------ 自定义的变量们 ----
  copyConfig =
    css:
      files: [{
        expand: true
        cwd: '_tmp/css/'
        src: ['**']
        dest: 'dist/css/'
      }
      ,{
        src: ['css/**/*']
        dest: 'dist/'
      }]

    js:
      files: [{
        expand: true
        cwd: '_tmp/js/'
        src: ['*.js']
        dest: 'dist/js/'
      }]

    img:
      files: [{
        expand: true
        src: ['img/**/*']
        dest: 'dist/'
      }]

    fonts:
      files: [{
        src: ['fonts/*']
        dest: 'dist/'
        expand: true
        flatten: false
      }]

    production: {}

    to_site:
      files: [{
        expand: true
        cwd: 'dist'
        src: ['**']
        dest: jekyllSiteDist
      }]

  copyConfig.production.files =
    [copyConfig.js.files, copyConfig.css.files, copyConfig.fonts.files, copyConfig.img.files].reduce((result, arr)->
      return result.concat(arr)
    )

  #console.log(copyConfig)

  concatConfig =
    vendor:
      src: [
        #'bower_components/zepto/zepto.min.js',
        'bower_components/jquery/dist/jquery.min.js',
        #'src/js/_components/jquery/jquery.min.js',
        #'src/js/_components/fastclick/lib/fastclick.js',
        #'src/js/_components/modernizr/modernizr.js',
        #'src/js/_components/nprogress/nprogress.js',
        #'src/js/_components/jquery-pjax/jquery.pjax.js',
        'src/js/prism_build.js'
        #'src/js/_components/perfect-scrollbar-0.4.8.min.js'
      ],
      dest: 'dist/js/vendor.js'

    script:
      src: [
        'js/*.js'
      ],
      dest: 'dist/js/build.js'

    tagclound:
      src: ['src/js/d3.js', 'js/pages/tagclound.js']
      dest: 'dist/js/tagclound.js'


  # start grunt config  ------------------------
  grunt.initConfig
    pkg: grunt.file.readJSON("package.json")
    notify:
      to_site:
        options:
          title: "移动文件错误"

    sass:
      dev:
        files: [
          cwd: 'src/sass'
          src: ["style.scss"]
          dest: '_tmp/css'
          expand: true
          ext: ".css"
        ]

    watch:
      sass:
        files: ['src/**/*.{sass,scss}']
        tasks: ['sass:dev', 'copy:css', 'autoprefixer', 'copy:to_site']
      js:
        files: ['js/*.js']
        tasks: ['concat:script', 'copy:js', 'copy:to_site']
      #pagesJs:
        #files: ['js/pages/*.js']
        #tasks: ['concat:tagclound', 'copy:js', 'copy:to_site']

    # 定义在上面
    copy: copyConfig

    concat: concatConfig

    concurrent:
      options:
        logConcurrentOutput: true

      dev:
        tasks: [
          "watch:sass"
        ]

    jshint:
      files: ["assets_crm/js/common.js"]
      ignore_warning:
        options:
          "-W015": true

        src: ["**/*.js"]

      options:
        jshintrc: true

    ###
    Autoprefix all CSS files
    ###
    autoprefixer:
      options:
        browsers: [
          "last 2 version"
          "safari 6"
          "ie 9"
          "opera 12.1"
          "ios 6"
          "android 4"
          "> 1%"
        ]

      build:
        src: "dist/css/style.css"



  grunt.loadNpmTasks "grunt-contrib-watch"
  grunt.loadNpmTasks "grunt-contrib-sass"
  grunt.loadNpmTasks "grunt-contrib-copy"
  grunt.loadNpmTasks "grunt-contrib-concat"

  #grunt.loadNpmTasks("grunt-contrib-jshint");
  grunt.loadNpmTasks "grunt-autoprefixer"
  grunt.loadNpmTasks "grunt-concurrent"
  grunt.loadNpmTasks "grunt-notify"

  #grunt.loadNpmTasks("grunt-contrib-qunit");
  grunt.loadNpmTasks "grunt-newer"

  #grunt.registerTask("test", ["qunit"]);
  grunt.registerTask "default", "", ->
    grunt.log.writeln "jblog前端编译，具体任务请使用grunt -h或者查看Gruntfile.coffee"
    return

  grunt.registerTask "to_site", '移到jekyll的_site下', ()->
    grunt.log.writeln "移到jekyll的_site下" ,jekyllSiteDist
    grunt.task.run 'copy:to_site'

  grunt.registerTask "prod", ['sass', 'copy:css', 'concat', 'autoprefixer:build', 'copy:to_site']

  grunt.registerTask "build", ['prod', 'copy:img']

  grunt.registerTask "design", "", ->
    grunt.log.writeln ""
    grunt.task.run 'concurrent:dev'
    return
  return
